package com.zbcz.crm.adapter

import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.zbcz.crm.MarketManagerActivity
import com.zbcz.crm.R
import com.zbcz.crm.RelatedAccessoriesActivity
import com.zbcz.crm.bean.ProjectBean
import com.zbcz.crm.databinding.ItemProjectListBinding
import com.zbcz.crm.util.onSingleClick
import com.zbcz.crm.util.setRoundedCorners

class ProjectBeanListAdapter(val mDatas: List<ProjectBean>) :
    RecyclerView.Adapter<ProjectBeanListAdapter.ProjectBeanHolder>() {

    companion object {
        // 获取项目类型描述的方法
        fun getProjectTypeDescription(type: Int?): String {
            return when (type) {
                1 -> "集成项目"
                2 -> "集成（施工）项目"
                3 -> "软件项目"
                4 -> "硬件产品项目"
                5 -> "硬件集成类项目"
                6 -> "软件集成项目"
                7 -> "其他项目"
                else -> "未知项目类型"
            }
        }

        const val PROJECT_BEAN = "project_bean"
    }

    private val expandedStates = mutableMapOf<Int, Boolean>()

    inner class ProjectBeanHolder(private val binding: ItemProjectListBinding) :
        ViewHolder(binding.root) {

        init {
            binding.tvFold.setOnClickListener {
                toggleItemExpansion(adapterPosition)
            }
            binding.inContent.tvProjectNameValue.onSingleClick {
                binding.root.context?.let { context: Context ->
                    context.startActivity(
                        Intent(
                            context,
                            MarketManagerActivity::class.java
                        ).apply {
                            putExtra(PROJECT_BEAN, mDatas[adapterPosition])
                        })
                }
            }
        }

        fun bind(bean: ProjectBean) {
            binding.inContent.tvProjectNameValue.text = bean.projectName
            binding.inContent.tvProjectTypeValue.text =
                getProjectTypeDescription(bean.projectType?.toIntOrNull())
            binding.inContent.tvProjectPreMoneyValue.text = bean.estimatedAmount
            binding.inContent.tvProjectRealMoneyValue.text = bean.actualAmount
            binding.inContent.tvProjectStatusValue.text =
                if ("1" == bean.projectStatus) "已完成" else "进行中"
            binding.inContent.tvProjectPreMoneyValue.setTextColor(
                ContextCompat.getColor(
                    binding.root.context,
                    if ("1" == bean.projectStatus) R.color.finish_color else R.color.running_color
                )
            )
            binding.inContent.tvProjectPrincipalValue.text = bean.leader
            binding.inContent.root.setRoundedCorners(45f)
            binding.tvFold.setOnClickListener {
                toggleItemExpansion(adapterPosition)
            }
        }

        fun setExpanded(expanded: Boolean) {
            var visible = if (expanded) View.VISIBLE else View.GONE
            binding.inContent.clProjectPreMoney.visibility = visible
            binding.inContent.clProjectRealMoney.visibility = visible
            binding.inContent.clProjectPrincipal.visibility = visible
            binding.inContent.clProjectStatus.visibility = visible
        }

    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ProjectBeanHolder {
        val inflater = LayoutInflater.from(parent.context)
        val binding = ItemProjectListBinding.inflate(inflater, parent, false)
        return ProjectBeanHolder(binding)
    }

    override fun getItemCount(): Int {
        return mDatas.size
    }

    override fun onBindViewHolder(holder: ProjectBeanHolder, position: Int) {
        holder.bind(mDatas[position])
        // 设置展开或折叠的状态
        holder.setExpanded(expandedStates[position] ?: true)
    }

    private fun toggleItemExpansion(position: Int) {
        expandedStates[position] = !(expandedStates[position] ?: true)
        notifyItemChanged(position)
    }
}